package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.jackysoft.data.AbstractDataProvider;
import org.jackysoft.entity.HtmlFormElement;
import org.jackysoft.mapper.HtmlFormElementMapper;
import org.jackysoft.mapper.OsWorkflowRunnerMapper;
import org.jackysoft.query.QueryBuilder;
import org.jackysoft.query.RunnerQuery;
import org.jackysoft.service.HtmlFormElementService;
import org.jackysoft.service.OsVariableService;
import org.jackysoft.util.CollectionUtils;
import org.jackysoft.util.WorkflowConstant;
import org.springframework.stereotype.Service;

@Service
public class HtmlFormElementServiceImpl extends
		AbstractDataProvider<HtmlFormElement> implements HtmlFormElementService {

	private @Resource
	OsVariableService varService;

	@Resource
	HtmlFormElementMapper mapper;
	@Resource
	OsWorkflowRunnerMapper osEntryMapper;
		
	
	public HtmlFormElementServiceImpl() {
		super();
	}

	@Override
	public void saveFormElement(final long workflowId, final boolean effect,
			final String template,
			final List<HtmlFormElement> formElements) {
		if (!effect)return;
		osEntryMapper.setHtmlTemplate(workflowId, template);
	   		
		
		HtmlFormElement example = new HtmlFormElement();
		example.setName("标题内容");
		HtmlFormElement titleElement = CollectionUtils.find(formElements,example);
		if (titleElement != null)
			varService.setVariable(workflowId,
					WorkflowConstant.FORM_TITLE.toString(),
					titleElement.getValue());
		
		for (HtmlFormElement s : formElements) {
			mapper.inert(s);
		}
		
	}

	@Override
	public boolean hasElement(long refid) {
		int c = mapper.countOfWorkflow(refid);
		return c>0;
	}

	@Override
	public Collection<HtmlFormElement> findByWorkflow(long refid) {
	
		RunnerQuery query = QueryBuilder.build("refid", refid+"");
		List<HtmlFormElement> alist = mapper.query(query);
		return alist;
	}

}
